package cn.stylefeng.guns.sys.core.util.common;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.model.StorageClass;
import com.qcloud.cos.region.Region;

public class TencentCloud {

	//初始化参数
	// private static final String appid = "1253530380";
	public static final String SecretId = "AKIDUmqgtvOGbhhQ9dy1qPjnbWGk2FD6oSr0";
	
	public static final String SecretKey = "4xRgeCXGkyymLejaQ5Wmq0Ply2wc4LVR";
	
	public static final String Bucket = "hybn-1259512846";
	
    //地域：成都（西南)
	private static final String cd = "ap-chengdu";
	
	private static TencentCloud instance = null;
	private TencentCloud() {};
	
	
	
	//构造单例模式
	public static TencentCloud getInstance() {
		
		if (instance == null) {
			instance = new TencentCloud();
		}
		
		return instance;	
	}
	
	
	/**生成 cos客户端
	 * @return
	 */
	public static COSClient getCosClient() {
		
		
		
	// 1 初始化用户身份信息（secretId, secretKey）
	COSCredentials cred = new BasicCOSCredentials(SecretId, SecretKey);
	
	// 2 设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
	// clientConfig中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者接口文档 FAQ 中说明。
	ClientConfig clientConfig = new ClientConfig(new Region(cd));
	
	// 3 生成 cos 客户端。
	COSClient cosClient = new COSClient(cred, clientConfig);
	// bucket的命名规则为{name}-{appid} ，此处填写的存储桶名称必须为此格式
	return cosClient;
	}
	
	
	
	
	
	/** 上传文件
	 * @param file
	 * @param key
	 * @return
	 */
	public static String  uploadFile(String Bucket, String key, File file) {
		
		//String key = "doc/text.txt";
		PutObjectRequest putObjectRequest = new PutObjectRequest(Bucket, key, file);
		// 设置存储类型, 默认是标准(Standard), 低频(standard_ia)
		putObjectRequest.setStorageClass(StorageClass.Standard_IA);
		try {
		    PutObjectResult putObjectResult = getCosClient().putObject(putObjectRequest);
		    // putobjectResult会返回文件的etag
		    String etag = putObjectResult.getETag(); 
		   System.out.println(etag);
		   System.out.println(putObjectRequest.getKey());
		   return key;
		   
		} catch (CosServiceException e) {
		    e.printStackTrace();
		    System.out.println("错误信息是："+e.getMessage());
		} catch (CosClientException e) {
		    e.printStackTrace();
		    System.out.println("错误信息是："+e.getMessage());
		}

		
		getCosClient().shutdown();
		return null ;

	}
	
	
	
	/** 支持传入输入流，需要指定长度
	 * @param file
	 * @param key
	 * @return
	 */
	public static String uploadStream(File file ,String key,Integer length) {
		
		
		InputStream input = new ByteArrayInputStream(new byte[10]);
		ObjectMetadata objectMetadata = new ObjectMetadata();
		
		
		// 从输入流上传必须制定content length, 否则http客户端可能会缓存所有数据，存在内存OOM的情况
		objectMetadata.setContentLength(length);
		
		
		// 设置contenttype默认下载时根据cos路径key的后缀返回响应的contenttype, 上传时设置contenttype会覆盖默认值
		objectMetadata.setContentType("image/jpeg");

		PutObjectRequest putObjectRequest =
		        new PutObjectRequest(Bucket, key, input, objectMetadata);
		// 设置存储类型, 默认是标准(Standard), 低频(standard_ia)
		
		putObjectRequest.setStorageClass(StorageClass.Standard_IA);
		try {
		    PutObjectResult putObjectResult = getCosClient().putObject(putObjectRequest);
		    // putobjectResult会返回文件的etag
		    String etag = putObjectResult.getETag();
		    System.out.println(etag);
		    
		    return etag;
		} catch (CosServiceException e) {
		    e.printStackTrace();
		} catch (CosClientException e) {
		    e.printStackTrace();
		}

		// 关闭客户端        
		getCosClient().shutdown();
		return null;
	
	}
	
	
	
	/**删除 COS 上指定路径的文件
	 * @param key
	 * @return
	 */
	public static boolean deleteFile(String key) {
		
		try {
			getCosClient().deleteObject(Bucket, key);
			return true;
			
		} catch (CosServiceException e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}catch(CosClientException e) {
			
			e.printStackTrace();
			return false;
		}
		
	}
	
}
